package org.idol.generator.codegen.service;

import org.idol.generator.codegen.service.method.AbstractJavaInterfaceMethodGenerator;
import org.idol.generator.codegen.service.method.QueryByIdGenerator;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.AbstractJavaGenerator;
import org.mybatis.generator.config.PropertyRegistry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
import static org.mybatis.generator.internal.util.messages.Messages.getString;

/**
 * @author coco
 *  服务端接口生成器
 * @date 2020-06-22 10:43
 **/
public class ServiceInterfaceGenerator extends AbstractJavaGenerator {


    public ServiceInterfaceGenerator(String project) {
        super(project);
    }

    @Override
    public List<CompilationUnit> getCompilationUnits() {
        FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
        progressCallback.startTask(getString(
                "Progress.19", table.toString())); //$NON-NLS-1$
        CommentGenerator commentGenerator = context.getCommentGenerator();

        FullyQualifiedJavaType type = new FullyQualifiedJavaType(
                introspectedTable.getServiceJavaInterfaceType());

        Interface interfaze = new Interface(type);
        interfaze.setVisibility(JavaVisibility.PUBLIC);
        commentGenerator.addJavaFileComment(interfaze);

        String rootInterface = context.getProperty("rootServiceInterface");
        if (!stringHasValue(rootInterface)) {
            rootInterface = introspectedTable
                    .getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
            if (!stringHasValue(rootInterface)) {
                rootInterface = context.getJavaClientGeneratorConfiguration()
                        .getProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
            }
        }

        if (stringHasValue(rootInterface)) {
            FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(
                    rootInterface);
            FullyQualifiedJavaType parameterType = introspectedTable.getRules()
                    .calculateAllFieldsClass();
            fqjt.addTypeArgument(parameterType);
            FullyQualifiedJavaType exampleType = new FullyQualifiedJavaType(
                    introspectedTable.getExampleType());
            fqjt.addTypeArgument(exampleType);
            interfaze.addSuperInterface(fqjt);
            interfaze.addImportedType(fqjt);
            interfaze.addImportedType(parameterType);
            interfaze.addImportedType(exampleType);
        }

        //addQueryByIdMethod(interfaze);

        List<CompilationUnit> answer = new ArrayList<>();
        if (context.getPlugins().clientServiceGenerated(interfaze, introspectedTable)) {
            answer.add(interfaze);
        }

        List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits();
        if (extraCompilationUnits != null) {
            answer.addAll(extraCompilationUnits);
        }

        return answer;
    }

    protected void addQueryByIdMethod(Interface interfaze) {
        AbstractJavaInterfaceMethodGenerator methodGenerator = new QueryByIdGenerator();
        initializeAndExecuteGenerator(methodGenerator, interfaze);
    }


    protected void initializeAndExecuteGenerator(
            AbstractJavaInterfaceMethodGenerator methodGenerator,
            Interface interfaze) {
        methodGenerator.setContext(context);
        methodGenerator.setIntrospectedTable(introspectedTable);
        methodGenerator.setProgressCallback(progressCallback);
        methodGenerator.setWarnings(warnings);
        methodGenerator.addInterfaceElements(interfaze);
    }

    public List<CompilationUnit> getExtraCompilationUnits() {
        return Collections.emptyList();
    }
}
